package com.yj.springbootjava8.date;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;

public class Demo01 {
    public static void main(String[] args) {
//        localDateTest();
//        localTimeTest();
//        localDateTimeTest();
        durationTest();
        periodTest();
    }

    // YYYY-mm-dd
    public static void localDateTest(){

        LocalDate now = LocalDate.now();
        System.out.println("查看当前日期时间："+now);
        System.out.println("获取指定的年："+now.getYear());
        System.out.println("获取指定的月："+now.getMonth());
        System.out.println("获取指定的日："+now.getDayOfMonth());
        System.out.println("获取后天的时间："+now.plusDays(2));
        System.out.println("获取前天的时间："+now.minusDays(2));


        //获取指定年月日日期
        LocalDate of = LocalDate.of(2018, 4, 20);
        System.out.println("获取指定时间日期："+of);
        int dayNums = of.lengthOfMonth();//查看这个月有多少天
        System.out.println("这个月有多少天："+dayNums);
        int yearNums = of.lengthOfYear();//查看这一年有多少天
        System.out.println("这一年有多少天："+yearNums);
        System.out.println("判断是否为闰年:"+of.isLeapYear()); //判断是否为闰年



        String str1 = now.format(DateTimeFormatter.BASIC_ISO_DATE);
        String str2 = now.format(DateTimeFormatter.ISO_DATE);
        System.out.println("将日期格式转为字符串："+str1);
        System.out.println("将日期格式转为指定格式的字符串："+str2);

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("YYYY-MM-dd");
        String formatStr = now.format(dtf);
        System.out.println("将日期格式转化为指定的字符串格式："+formatStr);
        LocalDate parse = LocalDate.parse(formatStr, dtf);
        System.out.println("将字符串转化为日期格式："+parse);


    }

    //获取当前的时间 hh:MM:ss
    public static void localTimeTest(){

        LocalTime now = LocalTime.now(); //获取当前的时间
        System.out.println("获取当前的时间："+now);

        System.out.println("获取当前的小时："+now.getHour()); //获取当前的时间
        System.out.println("获取当前的分钟："+now.getMinute()); //获取当前的分钟
        System.out.println("获取当前的秒："+now.getSecond()); //获取当前的秒

    }

    //localDateTime 是localDate 和 localTime 的组合
    public static void localDateTimeTest(){

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime of = LocalDateTime.of(LocalDate.now(), LocalTime.now());
        System.out.println("当前的时间为："+now);
        System.out.println("当前的时间为："+of);

        System.out.println("获取当前时间的年月日部分："+now.toLocalDate());
        System.out.println("获取当前时间的时间部分："+now.toLocalTime());


    }

    //Duration 用于计算两个 LocalTime 或者 LocalDateTime 的时间差
    public static void durationTest(){

        LocalTime time1 = LocalTime.of(16, 05);
        LocalTime time2 = LocalTime.of(16, 30);
        Duration between = Duration.between(time1, time2);
        long l = between.get(ChronoUnit.SECONDS);
        System.out.println("两者的时间相差:"+l);


        Duration threeMinutes = Duration.ofMinutes(3);
        threeMinutes = Duration.of(3, ChronoUnit.MINUTES); // 创建了一个3分钟的Duration，两种创建方式等价
        System.out.println(threeMinutes);
    }

    //Period用于计算两个LocalDate之间的时长
    public static void periodTest(){
        LocalDate date1 = LocalDate.of(2018,4,22);
        LocalDate date2 = LocalDate.of(2018, 5, 23);
        Period period = Period.between(date1, date2);
        int monthsBetween = period.getMonths(); // 1
        int daysBetween = period.getDays(); // 1
        System.out.println(monthsBetween);
        System.out.println(daysBetween);
    }

}
